package ttc.sftp.activity;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import ttc.sftp.R;
import ttc.sftp.adapter.MyGirdAdapter;
import ttc.sftp.adapter.MyPagerAdapter;
import ttc.sftp.adapter.VR;
import ttc.sftp.adapter.contextmenu.IconContextMenu.IconContextMenuOnClickListener;
import ttc.sftp.data.InstalledApp;
import ttc.sftp.data.VFile;
import ttc.sftp.data.VFile.Status;
import ttc.sftp.search.AsyncTaskGetFileName;
import ttc.sftp.search.LocalSearchProvider;
import ttc.sftp.service.AsyncTaskDownload;
import ttc.sftp.service.UploadService;
import ttc.sftp.user.User;
import ttc.sftp.util.FileSupport;
import ttc.sftp.util.ZipSupport;
import android.app.Activity;
import android.app.Dialog;
import android.app.SearchManager;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.SlidingDrawer;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.SlidingDrawer.OnDrawerOpenListener;
import android.widget.TextView;
import android.widget.Toast;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;

/**
 * @author vietslbfow
 * 
 */
public class SFTPActivity extends Activity {
	public static SFTPActivity sftpActivity;
	// Search
	private TextView mTextView;
	private ListView mListView;
	//
	Button btnSFTP;
	Button btnLocal;
	Button btnFavorit;
	Button btnSearch;
	Button btnSlid;
	Button btnMultiselect;
	Button btnBack;
	Button btnRefresh;
	Button btnClearAll;
	TextView txtPath;

	GridView grwChosenFiles;
	SlidingDrawer slid;
	ViewPager myPager;
	MyPagerAdapter pageAdapter;
	MyGirdAdapter gridAdapter;
	Context mContext;
	AsyncTaskGetFileName asynGetFile;
	private SharedPreferences mPrefs;
	String username, password, host, stringPort;
	int port;
	ChannelSftp sftpChannel;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mainlayout);
		mContext = this;
		// ////////////////////////////////////////////
		/*
		 * Intent intent = getIntent(); Bundle bundle = intent.getExtras();
		 * username = bundle.getString("userNameKey"); password =
		 * bundle.getString("passwordkey"); host = bundle.getString("hostKey");
		 * stringPort = bundle.getString("portKey"); int port =
		 * Integer.parseInt(stringPort); a loginSftpServer(username, password,
		 * host, stringPort);
		 */

		//
		try {
			SharedPreferences sp = getSharedPreferences("Channel", 0);
			username = sp.getString("username", null);
			password = sp.getString("password", null);
			host = sp.getString("host", null);
			stringPort = sp.getString("stringPort", null);
			Log.v("viet", username + password + host + stringPort);
			if (username != null)
				loginSftpServer(username, password, host, stringPort);
		} catch (Exception e) {
		}
		// Button Tabhost
		btnSFTP = (Button) findViewById(R.id.btnSFTP);
		btnLocal = (Button) findViewById(R.id.btnLocal);
		btnFavorit = (Button) findViewById(R.id.btnFavorit);
		btnSearch = (Button) findViewById(R.id.btnSearch);
		btnMultiselect = (Button) findViewById(R.id.btnMultiselect);
		btnBack = (Button) findViewById(R.id.btnBack);
		btnRefresh = (Button) findViewById(R.id.btnRefresh);
		btnClearAll = (Button) findViewById(R.id.btnClearAll);
		txtPath = (TextView) findViewById(R.id.txtPathLocal);
		// Set on click
		btnMultiselect.setOnClickListener(onclicklistener);
		btnBack.setOnClickListener(onclicklistener);
		btnRefresh.setOnClickListener(onclicklistener);
		btnClearAll.setOnClickListener(onclicklistener);

		slid = (SlidingDrawer) findViewById(R.id.slidingDrawer1);
		slid.setOnDrawerCloseListener(onDrawCloseSlid);
		slid.setOnDrawerOpenListener(onDrawOpenSlid);

		btnSlid = (Button) findViewById(R.id.handle);

		grwChosenFiles = (GridView) findViewById(R.id.grwChosenFiles);
		gridAdapter = new MyGirdAdapter(getLayoutInflater());
		grwChosenFiles.setAdapter(gridAdapter);
		grwChosenFiles.setOnItemClickListener(onitemclickgrwchosenfiles);

		pageAdapter = new MyPagerAdapter(sftpChannel, this, getResources());
		pageAdapter.SetContext(mContext);
		myPager = (ViewPager) findViewById(R.id.mypanelpager);
		myPager.setAdapter(pageAdapter);
		myPager.setCurrentItem(1);
		getButtonMain(1).setBackgroundResource(R.drawable.v_btnmain_s);
		myPager.setOnPageChangeListener(onPageChangeListener);
		InstalledApp.pm = getPackageManager();
		InstalledApp.apps = InstalledApp.pm.getInstalledPackages(0);

		sftpActivity = this;

		Intent intent = getIntent();

		pageAdapter.iconContextMenu.setOnClickListener(onclickIconMenuContext);
		if (Intent.ACTION_VIEW.equals(intent.getAction())) {
			Cursor cursor = managedQuery(intent.getData(), null, null, null,
					null);
		} else if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
			// handles a search query
			String query = intent.getStringExtra(SearchManager.QUERY);
			showResults(query);
		}
		ArrayList<String> array = new ArrayList<String>();
		array.add("heee");
		array.add("heeee");
		asynGetFile = new AsyncTaskGetFileName();
		asynGetFile.execute(array);
	}

	@Override
	protected void onPause() {

		SharedPreferences sp = getSharedPreferences("Channel", 0);
		SharedPreferences.Editor editor = sp.edit();
		editor.putString("username", username);
		editor.putString("password", password);
		editor.putString("host", host);
		editor.putString("stringPort", stringPort);
		editor.commit();
		super.onPause();
	}

	@Override
	protected void onStop() {

		SharedPreferences sp = getSharedPreferences("Channel", 0);
		SharedPreferences.Editor editor = sp.edit();
		editor.putString("username", username);
		editor.putString("password", password);
		editor.putString("host", host);
		editor.putString("stringPort", stringPort);
		editor.commit();
		super.onStop();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		MenuInflater myMenuInflater = getMenuInflater();
		myMenuInflater.inflate(R.menu.option_menu, menu);

		return true;
	}

	private Button getButtonMain(int id) {
		Button btn;
		switch (id) {
		case 0:
			btn = btnSFTP;
			break;
		case 1:
			btn = btnLocal;
			break;
		case 2:
			btn = btnFavorit;
			break;
		case 3:
			btn = btnSearch;
			break;
		default:
			btn = btnLocal;
		}
		return btn;
	}

	private OnPageChangeListener onPageChangeListener = new OnPageChangeListener() {

		@Override
		public void onPageSelected(int position) {
			// TODO Auto-generated method stub
			Button btn = getButtonMain(position);
			int idBgr = R.drawable.v_s_btnmain;
			int idBgr_s = R.drawable.v_btnmain_s;
			for (int i = 0; i < pageAdapter.nPage; i++) {
				/*
				 * if (i == (pageAdapter.nPage - 1)) { idBgr =
				 * R.drawable.s_btnbgrbtn1; idBgr_s = R.drawable.ibgrbtn1_s; }
				 */
				if (i != position)
					getButtonMain(i).setBackgroundResource(idBgr);
				else {
					getButtonMain(i).setBackgroundResource(idBgr_s);
				}
			}
			SetTextPath(pageAdapter.GetPathFolderLocal());
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {

			// TODO Auto-generated method stub

		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub

		}
	};

	public boolean onMenuItemSelected(int featureId, MenuItem item) {

		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
				.getMenuInfo();
		switch (item.getItemId()) {
		case R.id.search:
			onSearchRequested();
			break;
		case R.id.new1:
			showDialog(VR.id.DIALOG_LOGIN);
			break;
		case R.id.logOut:

			if (pageAdapter.getConnect() == true) {
				pageAdapter.setConnect(false);
				sftpChannel.disconnect();
				pageAdapter.setChannel(sftpChannel);
				myPager = (ViewPager) findViewById(R.id.mypanelpager);
				myPager.setAdapter(pageAdapter);
			}
			break;

		}
		if (gridAdapter.getCount() > 0)
			slid.setVisibility(SlidingDrawer.VISIBLE);
		return true;
	};

	@Override
	protected Dialog onCreateDialog(int id) {
		// TODO Auto-generated method stub
		final Dialog dialog = new Dialog(this);

		switch (id) {
		case VR.id.MENU_LOCAL:
		case VR.id.MENU_SFTP:
			return pageAdapter.iconContextMenu.createMenu("Operater");
		case VR.id.DIALOG_LOGIN:
			dialog.setTitle("Login");
			dialog.setContentView(R.layout.login);
			Button btnLogin = (Button) dialog.findViewById(R.id.btnLogin);
			Button btnCancel = (Button) dialog.findViewById(R.id.btnCancel);
			final EditText user = (EditText) dialog
					.findViewById(R.id.txtUsername);
			final EditText pass = (EditText) dialog
					.findViewById(R.id.txtPassword);
			final EditText host = (EditText) dialog.findViewById(R.id.txtHost);
			final EditText port = (EditText) dialog.findViewById(R.id.txtPort);
			btnLogin.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					ContentValues c = new ContentValues();
					if (user.getText().toString().equals("")
							|| pass.getText().toString().equals("")
							|| host.getText().toString().equals("")
							|| port.getText().toString().equals("")) {
						Toast.makeText(mContext, "missing information",
								Toast.LENGTH_SHORT).show();
					} else {
						new User(user.getText().toString(), pass.getText() // tao
																			// user
																			// va
																			// luu
																			// xuong
																			// csdl
								.toString(), host.getText().toString(), port
								.getText().toString(), mContext);
						myPager.setAdapter(pageAdapter);
						dialog.dismiss();
					}

				}
			});

			btnCancel.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					// User.show(mContext);
					dialog.dismiss();
				}
			});
			break;

		}
		return dialog;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		return super.onOptionsItemSelected(item);
	}

	private OnDrawerCloseListener onDrawCloseSlid = new OnDrawerCloseListener() {

		@Override
		public void onDrawerClosed() {
			// TODO Auto-generated method stub
			btnSlid.setBackgroundResource(R.drawable.slidup);
		}
	};

	private OnDrawerOpenListener onDrawOpenSlid = new OnDrawerOpenListener() {

		@Override
		public void onDrawerOpened() {
			// TODO Auto-generated method stub
			btnSlid.setBackgroundResource(R.drawable.sliddown);
		}
	};
	private OnItemClickListener onitemclickgrwchosenfiles = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View v, int position,
				long arg3) {
			Toast.makeText(mContext, String.valueOf(position),
					Toast.LENGTH_SHORT).show();
			final VFile file = (VFile) gridAdapter.getItem(position);
			switch (file.getStatus()) {
			case Copy:
				try {
					FileSupport.copy(
							file.getPath(),
							pageAdapter.GetPathFolderLocal() + "\\"
									+ file.getName());
					pageAdapter.Refresh();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				break;
			case Cut:
				try {
					FileSupport.rename(
							file.getPath(),
							pageAdapter.GetPathFolderLocal() + "\\"
									+ file.getName());
					pageAdapter.Refresh();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				break;

			default:
				break;
			}
			;
			gridAdapter.removeItem(position);
			gridAdapter.notifyDataSetChanged();
			if (gridAdapter.getCount() <= 0)
				slid.setVisibility(SlidingDrawer.INVISIBLE);

		}
	};
	private OnClickListener onclicklistener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btnBack:
				SetTextPath(pageAdapter.Back());
				break;
			case R.id.btnMultiselect:
				pageAdapter.myListAdapter.isMultiselect = !pageAdapter.myListAdapter.isMultiselect;
				pageAdapter.Refresh();
				break;
			case R.id.btnRefresh:
				pageAdapter.Refresh();
			case R.id.btnClearAll:
				gridAdapter.ClearAll();
				gridAdapter.notifyDataSetChanged();
				if (gridAdapter.getCount() <= 0)
					slid.setVisibility(SlidingDrawer.INVISIBLE);
				break;
			}
		}
	};

	private void SetTextPath(String path) {
		txtPath.setText(path);
	}

	private void showResults(String query) {
		Cursor cursor = managedQuery(LocalSearchProvider.CONTENT_URI, null,
				null, new String[] { query }, null);
		if (cursor == null) {

		} else {
			Log.v("viet",
					"change" + String.valueOf(pageAdapter.channel == null));
			pageAdapter.setDataLocal(cursor, sftpChannel);
		}
	}

	IconContextMenuOnClickListener onclickIconMenuContext = new IconContextMenuOnClickListener() {

		@Override
		public void onClick(int menuId) {
			final List<VFile> files = pageAdapter.GetItemsChecked();
			try {
				if (files.size() == 0)
					files.add(pageAdapter
							.GetItemAt(MyPagerAdapter.itemCurrentId));
			} catch (Exception e) {
			}
			switch (menuId) {
			case VR.id.CUT:
				gridAdapter.add(files, Status.Cut);
				break;
			case VR.id.COPY:
				gridAdapter.add(files, Status.Copy);
				break;
			case VR.id.RENAME:
				Rename(files);
				break;
			case VR.id.DELETE:
				Delete(files);
				break;
			case VR.id.COMPRESS:
				Compress(files);
				break;
			case VR.id.NEWFOLDER:
				Newfolder(files);
				break;
			case VR.id.SELECTALL:
				pageAdapter.SelectAll();
				break;
			case VR.id.DOWNLOAD:
				Object sftpFile = pageAdapter
						.getSftpItemSelected(pageAdapter.itemCurrentId);
				String folderClicked = ((com.jcraft.jsch.ChannelSftp.LsEntry) sftpFile)
						.getFilename();

				String currentPath = null;
				try {
					currentPath = (pageAdapter.channel).pwd();
				} catch (SftpException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				// download
				String filePath = currentPath + "/" + folderClicked;
				// goi service download
				/*
				 * Bundle bundle = new Bundle(); bundle.putString("usernameKey",
				 * username); bundle.putString("passwordKey", password);
				 * bundle.putString("hostKey", host);
				 * bundle.putString("portKey", stringPort);
				 * bundle.putString("filePathKey", filePath);
				 * bundle.putString("folderClickedKey", folderClicked);
				 * 
				 * Intent intent = new Intent(mContext, DownloadService.class);
				 * intent.putExtras(bundle); startService(intent);
				 */

				// goi asysTask de download
				ArrayList<String> array = new ArrayList<String>();
				array.add(username);
				array.add(password);
				array.add(host);
				array.add(stringPort);
				array.add(filePath);
				array.add(folderClicked);

				AsyncTaskDownload asyndownload = new AsyncTaskDownload();
				asyndownload.execute(array);
				Toast.makeText(mContext, "complete", Toast.LENGTH_SHORT).show();

				break;
			case VR.id.UPLOAD:

				Bundle bundle1 = new Bundle();
				bundle1.putString("usernameKey", username);
				bundle1.putString("passwordKey", password);
				bundle1.putString("hostKey", host);
				bundle1.putString("portKey", stringPort);
				bundle1.putString("fullfileparth", files.get(0).getPath());
				bundle1.putString("fileName", files.get(0).getName());

				Intent intent1 = new Intent(mContext, UploadService.class);
				intent1.putExtras(bundle1);
				startService(intent1);
				break;
			case VR.id.DELETEUSER:
				User user1 = (User) pageAdapter
						.getSftpItemSelected(pageAdapter.itemCurrentId);
				// Toast.makeText(getApplicationContext(),user1.getUsername(),Toast.LENGTH_SHORT).show();
				User.deleteByName(mContext, user1.getUsername());
				// User.deleteAllRow(mContext);
				myPager.setAdapter(pageAdapter);
				break;

			case VR.id.CONNECT:
				User user2 = (User) pageAdapter
						.getSftpItemSelected(pageAdapter.itemCurrentId);
				sftpChannel = loginSftpServer(user2.getUsername(),
						user2.getPassword(), user2.getHost(), user2.getPort());
				Toast.makeText(mContext, "connected", Toast.LENGTH_SHORT)
						.show();
				pageAdapter.setConnect(true);
				pageAdapter.setChannel(sftpChannel);
				myPager = (ViewPager) findViewById(R.id.mypanelpager);
				myPager.setAdapter(pageAdapter);

				//
				username = user2.getUsername();
				password = user2.getPassword();
				host = user2.getHost();
				stringPort = user2.getPort();
				Toast.makeText(mContext, "connected", Toast.LENGTH_SHORT)
						.show();

				break;
			case VR.id.DELETESFTPFILE:

				Object sftpFile1 = pageAdapter
						.getSftpItemSelected(pageAdapter.itemCurrentId);
				String folderClicked1 = ((com.jcraft.jsch.ChannelSftp.LsEntry) sftpFile1)
						.getFilename();

				String currentPath1 = null;
				try {
					currentPath1 = (pageAdapter.channel).pwd();
				} catch (SftpException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				// download
				String filePath1 = currentPath1 + "/" + folderClicked1;

				try {
					sftpChannel.rmdir(filePath1);

				} catch (Exception e) {
					// TODO: handle exception
					try {
						sftpChannel.rm(filePath1);
					} catch (SftpException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}

				myPager.setAdapter(pageAdapter);
				break;
			case VR.id.MAKESFTPFOLDER:
				String currentPath2 = "";
				try {
					currentPath1 = (pageAdapter.channel).pwd();
				} catch (SftpException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				// download
				String filePath2 = currentPath2;
				makeSftpFolder(sftpChannel, filePath2);
				Toast.makeText(mContext, "completed", Toast.LENGTH_SHORT)
						.show();
				break;

			}
			if (gridAdapter.getCount() > 0)
				slid.setVisibility(SlidingDrawer.VISIBLE);
		}
	};

	// login sftp server
	public ChannelSftp loginSftpServer(String username, String password,
			String host, String stringPort) {
		int port = Integer.parseInt(stringPort);
		JSch jsch = new JSch();
		Session session = null;
		try {
			session = jsch.getSession(username, host, port);
			session.setConfig("StrictHostKeyChecking", "no");
			session.setPassword(password);
			session.connect();
			Channel channel = session.openChannel("sftp");
			channel.connect();
			sftpChannel = (ChannelSftp) channel;

		} catch (JSchException e) {
			e.printStackTrace();
			Toast.makeText(getApplicationContext(), e.toString(),
					Toast.LENGTH_SHORT).show();
		}
		return sftpChannel;
	}

	// Function//
	private void Rename(final List<VFile> files) {
		final Dialog dialog = new Dialog(mContext);
		dialog.setContentView(R.layout.popup_rename);
		dialog.setCancelable(true);
		final EditText text = (EditText) dialog.findViewById(R.id.txtName);
		text.setText(files.get(0).getName());

		// set up button
		Button btnOK = (Button) dialog.findViewById(R.id.btnOK);
		Button btnCancel = (Button) dialog.findViewById(R.id.btnCancel);
		btnOK.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				try {
					FileSupport.rename(files.get(0).getPath(), files.get(0)
							.GetParent() + "\\" + text.getText());

				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				dialog.cancel();
				pageAdapter.Refresh();
			}
		});
		btnCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				dialog.cancel();
			}
		});
		// now that the dialog is set up, it's time to show it
		dialog.show();
	}

	private void Delete(final List<VFile> files) {
		for (VFile file : files) {
			FileSupport.delete(file.getPath());
		}
		pageAdapter.Refresh();
	}

	private void Newfolder(final List<VFile> files) {
		final Dialog dialog = new Dialog(mContext);
		dialog.setContentView(R.layout.popup_rename);
		dialog.setCancelable(true);
		final EditText text = (EditText) dialog.findViewById(R.id.txtName);
		text.setText("New folder");

		// set up button
		Button btnOK = (Button) dialog.findViewById(R.id.btnOK);
		Button btnCancel = (Button) dialog.findViewById(R.id.btnCancel);
		btnOK.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				try{
				File file = new File(files.get(0).GetParent()+"\\" + text.getText());
				file.mkdir();
				dialog.cancel();
				pageAdapter.Refresh();
				}catch (Exception e) {
					// TODO: handle exception
				}
			}
		});
		dialog.show();
	}

	private void Compress(final List<VFile> files) {
		final Dialog dialog = new Dialog(mContext);
		dialog.setContentView(R.layout.popup_compress);
		dialog.setCancelable(true);
		final EditText text = (EditText) dialog.findViewById(R.id.txtName);
		text.setText(files.get(0).getName());

		// set up button
		Button btnOK = (Button) dialog.findViewById(R.id.btnCompress);
		Button btnCancel = (Button) dialog.findViewById(R.id.btnCancel);
		btnOK.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String[] zipfiles = FileSupport.GetPathFiles(files);
				String name = files.get(0).getName();
				try {
					name = name.substring(0, name.lastIndexOf("."));
				} catch (Exception e) {
				}
				;
				try {
					ZipSupport.zip(zipfiles, files.get(0).GetParent() + "\\"
							+ name + ".zip");
					pageAdapter.Refresh();
				} catch (IOException e1) {
				}
				dialog.cancel();
				pageAdapter.Refresh();
			}
		});
		btnCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				dialog.cancel();
			}
		});
		// now that the dialog is set up, it's time to show it
		dialog.show();

	}

	// make folder sftp
	private void makeSftpFolder(final ChannelSftp sftpChannel,
			final String filePath) {
		final Dialog dialog = new Dialog(mContext);
		dialog.setContentView(R.layout.popup_rename);
		dialog.setCancelable(true);
		final EditText text = (EditText) dialog.findViewById(R.id.txtName);
		text.setText("");

		// set up button
		Button btnOK = (Button) dialog.findViewById(R.id.btnOK);
		Button btnCancel = (Button) dialog.findViewById(R.id.btnCancel);
		btnOK.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				try {
					sftpChannel.mkdir(filePath + File.separator
							+ text.getText().toString());
				} catch (SftpException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					Toast.makeText(mContext, "Can not make folder",
							Toast.LENGTH_SHORT).show();
				}

				dialog.cancel();
				pageAdapter.Refresh();
			}
		});
		btnCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				dialog.cancel();
			}
		});

		dialog.show();
	}

	// download sftp file

}